package com.chen.pspms.service.impl;

import com.chen.pspms.entity.Standardizationtable;
import com.chen.pspms.mapper.StandardizationMapper;
import com.chen.pspms.service.StandardizationService;
import com.chen.pspms.utils.ApplicationException;
import com.chen.pspms.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class StandardizationServiceImpl implements StandardizationService {
    @Autowired
    StandardizationMapper standardizationMapper;

    @Override
    public Integer save(Standardizationtable standardizationtable) throws ApplicationException {
        int row=0;
        try {
            row = standardizationMapper.save(standardizationtable);
        } catch (Exception e) {
            throw new ApplicationException("新增标准生产化失败", e);
        }
        return row;
    }

    @Override
    public Integer delete(Integer id) throws ApplicationException {
        int row=0;
        try {
            row = standardizationMapper.delete(id);
        } catch (Exception e) {
            throw new ApplicationException("删除标准生产化失败", e);
        }
        return row;
    }

    @Override
    @RedisCache(key = "#id")
    public Standardizationtable findyById(Integer id) throws ApplicationException {
        Standardizationtable standardizationtable;
        try {
            standardizationtable = standardizationMapper.findyById(id);
        } catch (Exception e) {
            throw new ApplicationException("查询标准生产化失败", e);
        }
        return standardizationtable;
    }

    @Override
    @RedisCache(key = "#hazardtype")
    public Standardizationtable findyByType(String hazardtype) throws ApplicationException {
        Standardizationtable standardizationtable;
        try {
            standardizationtable = standardizationMapper.findyByType(hazardtype);
        } catch (Exception e) {
            throw new ApplicationException("根据类型查询标准生产化失败", e);
        }
        return standardizationtable;
    }

    @Override
    public Integer update(Standardizationtable standardizationtable) throws ApplicationException {
        int row=0;
        try {
            row = standardizationMapper.update(standardizationtable);
        } catch (Exception e) {
            throw new ApplicationException("更新标准生产化失败", e);
        }
        return row;
    }

    @Override
    public List<Standardizationtable> findyAll() throws ApplicationException {
        List<Standardizationtable> standardizationtablelist;
        try {
            standardizationtablelist = standardizationMapper.findyAll();
        } catch (Exception e) {
            throw new ApplicationException("查询标准生产化列表失败", e);
        }
        return standardizationtablelist;
    }

    @Override
    public String getLevel(Double number, Standardizationtable standardizationtable) throws ApplicationException {
        String level;
        if(number<standardizationtable.getWarningvalue()){
            level = "无";
        }else if(number<standardizationtable.getLevel1value()){
            level = "预警";
        }else if(number<standardizationtable.getLevel2value()){
            level = "一级";
        }else if(number<standardizationtable.getLevel3value()){
            level = "二级";
        }else{
            level = "三级";
        }
        return level;
    }
}
